// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Permissive License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.


// README
//
// There are three steps to adding a property:
//
// 1. Create a member variable to store your property
// 2. Add the property to the type descriptor for this class
// 3. Add the get_ and set_ accessors for your property.
//
// Remember that all three are case sensitive!
//

Type.registerNamespace('GridControl');
GridControl.PagingStyle = function() {
  
    throw Error.invalidOperation();
}
GridControl.PagingStyle.prototype = {
    NumberPaging:0,
    NavBar:1
}
GridControl.PagingStyle.registerEnum("GridControl.PagingStyle", false);
GridControl.GridControlBehavior = function(element) {
    GridControl.GridControlBehavior.initializeBase(this,[element]);
    
    

    this.initialize = function() {
        GridControl.GridControlBehavior.callBaseMethod(this, 'initialize');
        
        
          this._initGrid();
       
           
    }
   
    this.dispose = function() {
       
        // TODO: add your cleanup code here
        
        GridControl.GridControlBehavior.callBaseMethod(this, 'dispose');
    }
    

   
    // TODO: (Step 3) Add your property accessors here.
    //
   
    
    this.get_ProcessingPage = function() {
        return this._ProcessingPage;
    }

    this.set_ProcessingPage = function(value) {
        this._ProcessingPage = value;
    }    
     this.get_Columns = function() {
        return this._Columns;
    }

    this.set_Columns = function(value) {
        this._Columns = value;
    } 
    this.get_GridControlId = function() {
        return this._GridControlId;
    }

    this.set_GridControlId = function(value) {
        this._GridControlId = value;
    }
    //tagname
     this.get_TagName = function() {
        return this._TagName;
    }

    this.set_TagName = function(value) {
        this._TagName = value;
    }    
    //totaltag
    this.get_totalTag = function() {
        return this._totalTag;
    }

    this.set_totalTag = function(value) {
        this._totalTag = value;
    }
    //Id
    this.get_IdRecord = function() {
        return this._IdRecord;
    }

    this.set_IdRecord= function(value) {
        this._IdRecord = value;
    }
    //Fields
     this.get_Fields = function() {
        return this._Fields;
    }

    this.set_Fields= function(value) {
        this._Fields= value;
    }
    //DataGrid
     this.get_Grid = function() {
        return this.DataGrid;
    }
    //
   
    //EnableRowSorting
       this.get_EnableRowSorting = function() {
        return this._EnableRowSorting;
    }

    this.set_EnableRowSorting= function(value) {
        this._EnableRowSorting= value;
    }
    //PageSize
    this.get_PageSize = function() {
        return this._PageSize;
    }

    this.set_PageSize= function(value) {
        this._PageSize= value;
    }
    //EnablePaging
     this.get_EnablePaging = function() {
        return this._EnablePaging;
    }

    this.set_EnablePaging= function(value) {
        this._EnablePaging= value;
    }
    //SelectMultiple
    this.get_SelectMultiple = function() {
        return this._SelectMultiple;
    }

    this.set_SelectMultiple= function(value) {
        this._SelectMultiple= value;
    }
    //WebServiceMethod
    this.get_WebServiceMethod = function() {
        return this._WebServiceMethod;
    }

    this.set_WebServiceMethod= function(value) {
        this._WebServiceMethod= value;
    }
    //WebServicePath
    this.get_WebServicePath = function() {
        return this._WebServicePath;
    }

    this.set_WebServicePath= function(value) {
        this._WebServicePath= value;
    }
    //PagingStyle
    this.get_PagingStyle =function() {
     
        return this._PagingStyle;
    }
    this.set_PagingStyle = function(value) {
       
        if (this._PagingStyle != value) {
            this._PagingStyle = value;
           
        }
    }
    
   
    this._initGrid = function() {
      
        var fldTno=new String(this._Fields);
        var objColumns=eval(this._Columns);
        var ArrayFields=fldTno.split(',');
      //auto dataIndex
      for(var i=0;i<objColumns.length;i++){
         if(!objColumns[i].dataIndex){
            objColumns[i].dataIndex=ArrayFields[i];
         }
      }
      
      if(this._SelectMultiple){
       sm= new Ext.grid.RowSelectionModel({singleSelect:false})
      }
      else{
       sm = new Ext.grid.RowSelectionModel({singleSelect:true})
      }
    
       cm = new Ext.grid.ColumnModel(objColumns);
       
	   
	  
	  if(this._WebServicePath ==null || this._WebServiceMethod ==null){
	        
	            if(this._ProcessingPage==null || this._ProcessingPage==""){
	                
	              var reader= new Ext.data.JsonReader(
                            {       
                                id: this._IdRecord, 
                                root: "records", 
                                totalProperty: "totalCount"
                            },
                            ArrayFields
                    )
                    
               
                    var ds = new Ext.data.Store({
                         
                            proxy: new Ext.data.HttpProxy({url: location.href}),
                            reader:reader,
                            remoteSort: this._EnablePaging//if paging is enabled then sorting is server-sided
                        });
                        
                        
                }
                else{
                           var reader= new Ext.data.XmlReader({
                   
                           record: this._TagName,
                           id: this._IdRecord,
                           totalRecords: this._totalTag
                       }, 
                        ArrayFields
                       );
               
                    var ds = new Ext.data.Store({
                         
                            proxy: new Ext.data.HttpProxy({url: this._ProcessingPage}),
                            reader:reader,
                            remoteSort: this._EnablePaging//if paging is enabled then sorting is server-sided
                        });
                
                
                
                }
        
        }
        else{
                 var reader= new Ext.data.DataSetReader({
                       totalRecords:this._totalTag,
                       id: this._IdRecord
                       
                   }, ArrayFields);

                
               
                var ds = new Ext.data.Store({
                // load using a webservice
                proxy: new Ext.data.WebserviceProxy(
                            {
                               _WebservicePath:this._WebServicePath,
                               _WebServiceMethod:this._WebServiceMethod
                            }
                   ),

                    
                    reader:reader,
                    remoteSort:  this._EnablePaging
                });
            
            }
        
            if(this._PageSize==null || this._PageSize ==0){
                this._PageSize=10;
            }
            
            grid = new Ext.grid.Grid(this._GridControlId, {
                            ds: ds,
                            cm: cm,
                            selModel: sm,
                            enableDragDrop:this._EnableRowSorting
                        });
           
           if(this._EnableRowSorting){
               var ddrow = new Ext.dd.DropTarget(grid.container, {
                    ddGroup : 'GridDD',
                    copy:false,
                    notifyDrop : function(dd, e, data){
                        var sm=grid.getSelectionModel();
                        var rows=sm.getSelections();
                
                        var cindex=dd.getDragData(e).rowIndex;
                        for (i = 0; i < rows.length; i++) {
                            rowData=ds.getById(rows[i].id);
                            if(!this.copy) {
                                ds.remove(ds.getById(rows[i].id));
                                ds.insert(cindex,rowData);
                            }
                        };
                    }
                });
            }
            grid.render();
            
            if(this._EnablePaging){
             
              var gridFoot = grid.getView().getFooterPanel(true);
              if(this._PagingStyle==GridControl.PagingStyle.NumberPaging){
                  var paging = new Ext.NumberPagingToolbar(gridFoot, ds, {pageSize: this._PageSize});
              }
              else{
                  var paging = new Ext.PagingToolbar(gridFoot, ds, {
                        pageSize: this._PageSize,
                        displayInfo: true,
                        displayMsg: 'Displaying records {0} - {1} of {2}',
                        emptyMsg: "No record to display"
                    });
              }
               this.PagingToolBar=paging;
            }
           
           
            grid.autosizeColumns=true;
        
            //ds.on('beforeload', this.onBeginUpdate);
            //ds.on('load', this.onEndUpdate);
             if(this._EnablePaging){
               ds.load({params:{start:0, limit:this._PageSize,sort:'',dir:''}});
             }
             else{
                ds.load({params:{start:0}});
             }
            this.DataGrid=grid;
    }
    this.onBeginUpdate=function(){
       Ext.MessageBox.show({
           title: 'Please wait...',
           msg: 'Loading',
           width:240,
           closable:false
       })
    }
    this.onEndUpdate=function(){
       Ext.MessageBox.hide();
    }
    // These are helper functions for communicating state back to the extender on the
    // server side.  They take or return a custom string that is available in your initialize method
    // and later.
    //
    this.getClientState = function() {
        var value = GridControl.GridControlBehavior.callBaseMethod(this, 'get_ClientState');                
        if (value == '') value = null;
        return value;
    }
     
    this.setClientState = function(value) {
        return GridControl.GridControlBehavior.callBaseMethod(this, 'set_ClientState',[value]);                
    }
  
 
}

GridControl.GridControlBehavior.registerClass('GridControl.GridControlBehavior',AjaxControlToolkit.BehaviorBase);

